package com.hp.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hp.context.Config;
import com.hp.dao.ResourcesTypeDao;
import com.hp.entity.ResourceTypeTreeVo;
import com.hp.entity.ResourcesType;
import com.hp.result.DataGridResultInfo;
import com.hp.result.ResultUtil;
import com.hp.result.SubmitResultInfo;
import com.hp.service.ResourcesTypeService;
import com.hp.utils.UUIDBuild;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class ResourcesTypeServiceImpl implements ResourcesTypeService {

    @Autowired
    private ResourcesTypeDao resourcesTypeDao;

    /**
     * java doc
     *
     * @param page          第几页
     * @param rows          每页显示的数
     * @return
     * @throws Exception
     */
    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo pageresourcetype(int page, int rows) throws Exception {
        Page<Object> startPage = PageHelper.startPage(page, rows);
        resourcesTypeDao.findAllByParentIsNotNull();
        DataGridResultInfo data = new DataGridResultInfo();
        data.setRows(startPage.getResult());
        data.setTotal((int) startPage.getTotal());
        return data;
    }




    /**
     * 添加资源类型
     * @param resourcesType
     * @return
     * @throws Exception
     */
    @Override
    public SubmitResultInfo savetype(ResourcesType resourcesType) throws Exception {

        //获取父类
        ResourcesType parentIsNull= resourcesTypeDao.findByParentIsNull();

        //关联
        resourcesType.setParentid(parentIsNull.getId());
        resourcesType.setCreatetime(new Date());
        resourcesType.setId(UUIDBuild.getUUID());

        //保存
        resourcesTypeDao.savatype(resourcesType);


        //Java new Object[]{}
        //C++/C new Object[10]{}
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,661,new Object[]{"资源类型"}));
    }

    /**
     * 查询类型通过id
     * @param id
     * @return
     * @throws Exception
     */
    @Transactional(readOnly = true)
    @Override
    public ResourcesType findTypeById(String id) throws Exception {
        return resourcesTypeDao.findTypeById(id);
    }

    @Override
    public SubmitResultInfo updatetype(ResourcesType resourcesType) throws Exception {

        resourcesTypeDao.updataType(resourcesType);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,666,new Object[]{"类型"}));
    }

    @Override
    public SubmitResultInfo deletetype(String typeid) throws Exception {

        resourcesTypeDao.deleteTypeById(typeid);
        return ResultUtil.createSubmitResult(ResultUtil.createSuccess(Config.MESSAGE,662,new Object[]{"类型"}));
    }

    @Override
    public List<ResourcesType> getResourcesTypes() {
        return resourcesTypeDao.getAll();

    }

    @Override
    public ResourceTypeTreeVo getResourceTypeTreeVo() throws Exception {
        ResourcesType parent = resourcesTypeDao
                .getParent();
        ResourceTypeTreeVo resourceTreeVo = new ResourceTypeTreeVo(parent);
        return resourceTreeVo;
    }


    @Transactional(readOnly = true)
    @Override
    public DataGridResultInfo getAllById(String typeid, int page, int rows, ResourcesType resources) throws Exception {
        Page<ResourcesType> startPage = PageHelper.startPage(page,
                rows);
        resourcesTypeDao.findAllByIdAndIsY(typeid);
        DataGridResultInfo dataGridResultInfo = new DataGridResultInfo();
        dataGridResultInfo.setRows(startPage.getResult());
        dataGridResultInfo.setTotal((int) startPage.getTotal());
        return dataGridResultInfo;
    }

}